package com.aixuexi.item.controller;

import com.aixuexi.item.entity.Item;
import com.aixuexi.item.service.ItemService;
import com.aixuexi.item.vo.ItemVo;
import com.aixuexi.item.vo.PageVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Item)表控制层
 *
 * @author makejava
 * @since 2022-02-15 09:33:17
 */
@RestController
@RequestMapping("item")
public class ItemController {
    /**
     * 服务对象
     */
    @Resource
    private ItemService itemService;

    /**
     * 分页查询
     *
     * @param item 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Item>> queryByPage(Item item, PageRequest pageRequest) {
        return ResponseEntity.ok(this.itemService.queryByPage(item, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Item> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.itemService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param item 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Item> add(Item item) {
        return ResponseEntity.ok(this.itemService.insert(item));
    }

    /**
     * 编辑数据
     *
     * @param item 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Item> edit(Item item) {
        return ResponseEntity.ok(this.itemService.update(item));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.itemService.deleteById(id));
    }


    @GetMapping("/getItems")
    public ResponseEntity<PageVo<ItemVo>> getItems(String categoryIds,
                                                 String itemName,
                                                 @RequestParam(defaultValue = "1") int currentPage,
                                                 @RequestParam(defaultValue = "10") int pageSize){
        PageVo<ItemVo> page =  itemService.getItems(categoryIds,itemName,currentPage,pageSize);
        return ResponseEntity.ok(page);
    }

}

